package com.future.mayi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.List;

public abstract class CommonAdapter<T> extends ArrayAdapter<T> {

	protected LayoutInflater mInflater;
	protected Context mContext;
	protected List<T> mDatas;
	protected final int mItemLayoutId;

	public CommonAdapter(Context context, int textViewResourceId,
                         List<T> objects) {
		super(context, textViewResourceId, objects);
		this.mContext = context;
		this.mInflater = LayoutInflater.from(mContext);
		this.mDatas = objects;
		this.mItemLayoutId = textViewResourceId;
	}

	public Context getContext() {
		return mContext;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder = getViewHolder(position, convertView,
				parent);
		convert(viewHolder, getItem(position));
		return viewHolder.getConvertView();

	}

	public abstract void convert(ViewHolder helper, T item);

	private ViewHolder getViewHolder(int position, View convertView,
			ViewGroup parent) {
		return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
				position);
	}

	public void addList(List<T> list) {
		if (list != null && list.size() > 0) {
			mDatas.addAll(list);
			notifyDataSetChanged();
		}
	}

	public void updateList(List<T> list) {
		mDatas.clear();
		if (list != null && list.size() > 0) {
			mDatas.addAll(list);
			notifyDataSetChanged();
		}else {
			notifyDataSetChanged();
		}
	}

	public void setList(List<T> list) {
		mDatas=list;
		notifyDataSetChanged();
	}

	public List<T> getlist() {
		return mDatas;
	}

}
